package com.globallogic.dailyplanner.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.globallogic.dailyplanner.dto.RoleEnum;
import com.globallogic.dailyplanner.dto.User;

public class UserDetailsImpl implements UserDetails{

	private static final long serialVersionUID = 2876129807749489954L;
	private User user;

	public UserDetailsImpl(User user) {
		this.user = user;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		final Set<GrantedAuthority> grantedAuthority = new HashSet<GrantedAuthority>();
		
		List<RoleEnum> userRoles = new ArrayList<RoleEnum>();
		userRoles.add(user.getRole());
		
		//TODO replace hard code with something nice
		if(!userRoles.isEmpty()){
			for(RoleEnum role : userRoles) {
				grantedAuthority.add(new SimpleGrantedAuthority("ROLE_"+role.name()));
			}
		}
		
		return grantedAuthority;
	}

	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getUsername();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return user.isActive();
	}

}
